﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    /*
     * 
     * An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following expression.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

     * 
     * 
     * */
    class Problem40 : IProblem
    {
        public string Calculate()
        {
            int i = 0;
            int digit = 0;

            long product = 1;



            int power = 0;

            while (power < 6)
            {
                i++;
                int digitsToAdd = CommonFunctions.NumberOfDigits(i);

                if (digit + digitsToAdd >= (int)Math.Pow(10, power))
                {                
                    int difference = (int)Math.Pow(10, power) - digit;
                    
                    int result = i % (int)Math.Pow(10, digitsToAdd - difference + 1);

                    int resultDigits = CommonFunctions.NumberOfDigits(result);
                    if (resultDigits > 1)
                    {
                        result /= (int)Math.Pow(10, resultDigits - 1);
                    }
                    product *= result;
                    Console.WriteLine(result);

                    power++;
                   
                }
                digit += digitsToAdd;
            }

            return product.ToString();
        }
    }
}
